package com.chixing.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 该类表示日期工具类
 *  实现了Date与字符串的相互转换
 */
public class DateUtil {

    //年月日时分秒：yyyy-MM-dd HH:mm:ss pattern （格式）
    public static final String DATE_PATTEN_YMDHMS = "yyyy-MM-dd HH:mm:ss";
    //		年月日时分：yyyy-MM-dd HH:mm
    public static final String DATE_PATTEN_YMDHM = "yyyy-MM-dd HH:mm";
    //		时分秒：HH:mm:ss
    public static final String DATE_PATTEN_HMS = "HH:mm:ss";
    //		时分：HH:mm
    public static final String DATE_PATTEN_HM = "HH:mm";
    public static final String DATE_PATTEN_YMD = "yyyy-MM-dd";
    public static final String PATTERN_YMDHMS = "yyyyMMddHHmmss";


    // Date 转换成 String
    public static String dateToString(Date date,String pattern){
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        String dateStr = dateFormat.format(date);
        return dateStr;
    }
    // String 转换成 Date
    public static Date stringToDate(String dateStr,String pattern){
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = dateFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}
